<?php
/**********************************************************************************
WIKINDX: Bibliographic Management system.
Copyright (C)

This program is free software; you can redistribute it and/or modify it under the terms 
of the GNU General Public License as published by the Free Software Foundation; either 
version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; 
if not, write to the 
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

The WIKINDX Team 2007
sirfragalot@users.sourceforge.net
**********************************************************************************/
/*****
*	Email class.
*****/
class EMAIL
{
// Constructor
	function EMAIL($db = FALSE, $vars = FALSE)
	{
		$this->db = $db;
		$this->vars = $vars;
		include_once("core/session/SESSION.php");
		$this->session = new SESSION();
		include_once("core/messages/MESSAGES.php");
		$this->messages = new MESSAGES();
		include("config.php");
		if(isset($WIKINDX_MAIL_RETURN_PATH))
			$this->returnPath = $WIKINDX_MAIL_RETURN_PATH;
		else
			$this->returnPath = FALSE;
	}
/**
* Send the email
*/
	function sendEmail($address, $subject, $message, $headers)
	{
		if(!@mail($address, $subject, $message, $headers, $this->returnPath))
			return FALSE;
		return TRUE;
	}
/** Start functions for emailing users their preferences or 'my wikindx' details.
* Called from core/user/USER.php
*/
	function userEdit()
	{
		$link = $this->scriptPath();
		if(array_key_exists('email', $this->vars))
		{
			$email = $this->vars['email'];
			$subject = "WIKINDX Registration Confirmation";
			$message = $this->messages->text("user", "emailText3") . "\n\nWIKINDX:\t\t$link\nUSERNAME:\t\t" . 
				trim($this->vars['uname']) . "\n";
			$headers = "From: WIKINDX\n";
			$headers .= "Reply-to: noReply\r\n";
			$this->sendEmail($email, $subject, $message, $headers);
		}
	}
/** Start functions for emailing resource add/edit operations to users
* Called from core/notify/NOTIFY.php
*/
	function notify($email, $subject, $message, $headers)
	{
		$this->sendEmail($email, $subject, $message, $headers);
	}
/**
* START functions for emailing news items to users
* Called from core/news/NEWS.php
*/
	function news($recordset, $title, $news)
	{
		$wikindxTitle = $this->session->getVar("setup_title");
		if(!$wikindxTitle)
			$wikindxTitle = "WIKINDX";
		$subject = "$wikindxTitle News";
		$message = "\n\n$title\n\n";
		$message .= "$news\n\n";
		$headers = "From: WIKINDX\n";
		$headers .= "Reply-to: noReply\r\n";
		while($row = $this->db->loopRecordset($recordset))
		{
			if(!$row['email']) // This should only happen if superadmin has not entered email
				continue;
			$email = $row['email'];
			$this->sendEmail($email, $subject, $message, $headers);
		}
	}
/**
* START functions for emailing username::password to forgetful user
* Called from core/authorize/AUTHORIZE.php
*/
	function forgetProcess($username, $password)
	{
		$password = time();
		$link = $this->scriptPath();
		$email = trim($this->vars['email']);
		$subject = "WIKINDX Password Reset";
		$message = $this->messages->text("user", "forget9");
		$message .= "\n\nWIKINDX:\t\t\t\t$link";
		$message .= "\nUSERNAME:\t\t\t\t" . $username;
		$message .= "\nTEMPORARY PASSWORD:\t\t" . $password;
		$message .= "\n";
		$headers = "From: WIKINDX\n";
		$headers .= "Reply-to: noReply\r\n";
		if(!$this->sendEmail($email, $subject, $message, $headers))
			return FALSE;
		return TRUE;
	}
/**
* START functions for user registration emails
* Called from core/admin/ADMINUSER.php
*/
	function registerUserAdd()
	{
		$link = $this->scriptPath();
//		$link = "http://" . $_SERVER['SERVER_NAME'] . $_SERVER['PHP_SELF'];
		$email = $this->vars['email'];
		$subject = "WIKINDX Registration Confirmation";
		$message = $this->messages->text("user", "emailText2") . "\n\nWIKINDX:\t\t$link\nUSERNAME:\t\t" . 
			trim($this->vars['username']) . "\nPASSWORD:\t\t" . trim($this->vars['password']) . "\n\n";
		$headers = "From: WIKINDX\n";
		$headers .= "Reply-to: noReply\r\n";
		$this->sendEmail($email, $subject, $message, $headers);
// If needed, email admin about new user
		$recordset = $this->db->select(array('WKX_config'), 'emailNewRegistrations');
		if($email = $this->db->fetchOne($recordset))
		{
			$message = "A new user has registered for" . "\n\nWIKINDX:\t\t$link\n\nUSERNAME:\t\t" . 
				trim($this->vars['username']) . "\n";
			if($this->vars['fullname'])
				$message .= "FULLNAME:\t\t" . trim($this->vars['fullname']) . "\n";
			$this->sendEmail($email, $subject, $message, $headers);
		}
	}
	function registerRequestManage($registerIds)
	{
		$scriptPath = $this->scriptPath();
		foreach($registerIds as $id => $value)
		{
			$resultset = $this->db->select(array('WKX_user_register'), array('email', 'hashKey'), 
				' WHERE ' . $this->db->formatField('id') . '=' . $this->db->tidyInput($id) . 
				' AND ' .  $this->db->formatField('confirmed') . '=' . $this->db->tidyInput('N'));
			$row = $this->db->fetchRow($resultset);
// Email user
			$email = $row['email'];
			$subject = "WIKINDX Registration Confirmation";
			if($value == 'accept')
			{
				$link = "$scriptPath?action=registerConfirm&hashKey=" . $row['hashKey'];
				$message = $this->messages->text("user", "emailText") . "\n\n" . $link . "\n\n";
			}
			else
				$message = $this->messages->text("user", "emailText5", " $scriptPath") . "\n\n";
			$headers = "From: WIKINDX\n";
			$headers .= "Reply-to: noReply\r\n";
			if(!$this->sendEmail($email, $subject, $message, $headers))
				return FALSE;
// Delete those declined and confirm those accepted
			if($value == 'accept')
				$this->db->updateSingle('WKX_user_register', $this->db->formatField('confirmed') . 
				'=' . $this->db->tidyInput('Y') . ' WHERE ' . $this->db->formatField('id') . 
				'=' . $this->db->tidyInput($id));
			else
				$this->db->delete('WKX_user_register', ' WHERE ' . $this->db->formatField('id') . 
				'=' . $this->db->tidyInput($id));
		}
		return TRUE;
	}
	function register($hashKey)
	{
		$scriptPath = $this->scriptPath();
		$subject = "WIKINDX Registration";
		$email = trim($this->vars['email']);
		$link = "$scriptPath?action=registerConfirm&hashKey=$hashKey";
		$message = $this->messages->text("user", "emailText") . "\n\n" . $link . "\n\n";
		$headers = "From: WIKINDX\n";
		$headers .= "Reply-to: noReply\r\n";
		if(!$this->sendEmail($email, $subject, $message, $headers))
			return FALSE;
		return $email;
	}
	function registerRequest()
	{
		$adminEmail = $this->db->fetchOne($this->db->select(array('WKX_config'), array('emailNewRegistrations')));
		if(!$adminEmail)
			return FALSE;
		$subject = "WIKINDX Registration Request";
		$scriptPath = $this->scriptPath();
		$message = $this->messages->text("user", "emailText4") . "\n\n";
		$headers = "From: WIKINDX\n";
		$headers .= "Reply-to: noReply\r\n";
		if(!$this->sendEmail($adminEmail, $subject, $message, $headers))
			return FALSE;
		return TRUE;
	}
/**
* START functions for emailing a resource to a friend
* Called from index.php
*/
// Display form in pop-up for emailing a link to a friend
	function emailFriendDisplay($error = FALSE)
	{
		include_once("core/template/TEMPLATE.php");
		$this->template = new TEMPLATE('content');
		include_once("core/html/CLOSEPOPUP.php");
		include_once("core/html/FORM.php");
		include_once("core/html/MISC.php");
		if(array_key_exists('id', $this->vars))
		{
			global $_SERVER;
			$hyperlink = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'] . "?action=resourceView&id=" . $this->vars['id'];
		}
		$this->template->setVar('heading', $this->messages->text("heading", "emailFriend"));
		$error ? $pString = $error : $pString = '';
		$this->template->setVar('formStart', FORM::formHeader("emailFriend"));
		$address = $this->session->getVar("emailFriend_address");
		$subject = $this->session->getVar("emailFriend_subject");
		if(isset($hyperlink))
			$text = "\n\n$hyperlink";
		else
		{
			$text = $this->session->getVar("emailFriend_text");
		}
		$pString .= MISC::p(FORM::textInput($this->messages->text("misc", "emailFriendAddress"), 
			"emailFriend_address", $address, 40));
		$pString .= MISC::p(FORM::textInput($this->messages->text("misc", "emailFriendSubject"), 
			"emailFriend_subject", $subject, 80));
		$pString .= MISC::p(FORM::textareaInput($this->messages->text("misc", "emailFriendText"), 
			"emailFriend_text", $text, 80, 10));
		$pString .= MISC::p(FORM::formSubmit("email"), FALSE, 'right');
		$this->template->setVar('body', $pString);
		$this->template->setVar('formEnd', FORM::formEnd());
		new CLOSEPOPUP($this->template->process());
	}
// Send email
	function emailFriend()
	{
		list($address, $subject, $text) = $this->checkInput();
		include_once("core/template/TEMPLATE.php");
		$this->template = new TEMPLATE('content');
		include_once("core/messages/SUCCESS.php");
		$this->success = new SUCCESS();
		include_once("core/html/CLOSEPOPUP.php");
		$this->template->setVar('heading', $this->messages->text("heading", "emailFriend"));
		$headers = "From: WIKINDX\n";
		$headers .= "Reply-to: noReply\r\n";
		$this->sendEmail($address, $subject, $text, $headers);
		$this->session->clearArray('emailFriend');
		$this->template->setVar('body', $this->success->text('emailFriend'));
		new CLOSEPOPUP($this->template->process());
	}
	function checkInput()
	{
		include_once("core/messages/ERRORS.php");
		$errors = new ERRORS();
		$address = $subject = $text = FALSE;
		if(array_key_exists('emailFriend_address', $this->vars))
			$address = trim($this->vars['emailFriend_address']);
		if(array_key_exists('emailFriend_subject', $this->vars))
			$subject = trim($this->vars['emailFriend_subject']);
		if(array_key_exists('emailFriend_text', $this->vars))
			$text = trim($this->vars['emailFriend_text']);
		$this->session->setVar('emailFriend_address', $address);
		$this->session->setVar('emailFriend_subject', $subject);
		$this->session->setVar('emailFriend_text', $text);
		if(!$address || !$subject || !$text)
			$this->badInput('emailFriendDisplay', $errors->text('inputError', 'missing'));
		return array($address, $subject, $text);
	}
/**
* Get scriptPath
*/
	function scriptPath()
	{
		global $_SERVER;
		$referer = split("\?", $_SERVER['HTTP_REFERER']);
		return $referer[0];
	}
// Error handling
	function badInput($function, $error)
	{
		include_once("core/html/CLOSEPOPUP.php");
		new CLOSEPOPUP($this->$function($error));
	}
}
?>